/*
 * @copyright 2011 Gerald Jacobson
 * @license GNU General Public License
 * 
 * This file is part of GameTimer.
 *
 * GameTimer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GameTimer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GameTimer. If not, see <http://www.gnu.org/licenses/>.
 */
package org.amphiption.gametimer.dao;

import java.util.ArrayList;
import java.util.List;

import org.amphiption.gametimer.entity.Player;
import org.amphiption.gametimer.entity.TimerConfiguration;

import android.content.Context;
import android.database.Cursor;

/**
 * This class is responsible of all database player access.
 * 
 * @author amphiprion
 * 
 */
public class PlayerDao extends AbstractDao {
	/** The singleton. */
	private static PlayerDao instance;

	/**
	 * Hidden constructor.
	 * 
	 * @param context
	 *            the application context
	 */
	private PlayerDao(Context context) {
		super(context);
	}

	/**
	 * Return the singleton.
	 * 
	 * @param context
	 *            the application context
	 * @return the singleton
	 */
	public static PlayerDao getInstance(Context context) {
		if (instance == null) {
			instance = new PlayerDao(context);
		}
		return instance;
	}

	/**
	 * Return all players for a timer coniguration.
	 * 
	 * @return the player list
	 */
	public List<Player> getPlayers(TimerConfiguration timer) {

		String sql = "SELECT " + Player.DbField.ID + "," + Player.DbField.NAME + "," + Player.DbField.COLOR + "," + Player.DbField.DURATION + " from PLAYER where "
				+ Player.DbField.FK_CONFIGURATION + "=? order by " + Player.DbField.NAME + " asc";
		Cursor cursor = getDatabase().rawQuery(sql, new String[] { timer.getId() });
		ArrayList<Player> result = new ArrayList<Player>();
		if (cursor.moveToFirst()) {
			do {
				Player entity = new Player(cursor.getString(0));
				entity.setName(cursor.getString(1));
				entity.setColor(cursor.getInt(2));
				entity.setDuration(cursor.getInt(3));
				result.add(entity);
			} while (cursor.moveToNext());
		}
		cursor.close();
		return result;
	}

	/**
	 * Persist a new player.
	 * 
	 * @param player
	 *            the new player
	 */
	public void create(Player player) {
		String sql = "insert into PLAYER (" + Player.DbField.ID + "," + Player.DbField.NAME + "," + Player.DbField.COLOR + "," + Player.DbField.DURATION + ","
				+ Player.DbField.FK_CONFIGURATION + ") values (?,?,?,?,?)";
		Object[] params = new Object[5];
		params[0] = player.getId();
		params[1] = player.getName();
		params[2] = player.getColor();
		params[3] = player.getDuration();
		params[4] = player.getTimerConfigurationId();

		execSQL(sql, params);

	}

}
